﻿using System;
using BigfootWeb;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;

namespace BigfootDNN.Mvc
{

    /// <summary>
    /// This class represents the route information. It includes controller, action, portalid, tabid, and moduleid
    /// This info is by default created from the url in the route parameter.
    /// </summary>
    public class RouteInfo
    {

        /// <summary>
        /// Empty constructor. All data must be specified in order for the application to work properly
        /// </summary>
        public RouteInfo() { }

        /// <summary>
        /// Creates a new RouteInfo object by specifying. Defaults the Controller and Action to the default values
        /// </summary>
        public RouteInfo(ModuleInfo modInfo, DnnApp app)
        {
            Controller = "Home";
            Action = "Index";
            ModData = modInfo;
            App = app;
        }

        /// <summary>
        /// Creates a new RouteInfo object by specifying the full range of parameters
        /// </summary>
        public RouteInfo(string controller, string action, ModuleInfo modInfo, DnnApp app)
        {
            Controller = controller;
            Action = action;
            ModData = modInfo;
            App = app;
        }

        /// <summary>
        /// Name of the controller for this route
        /// </summary>
        public string Controller;

        /// <summary>
        /// Name of the action within the controller
        /// </summary>
        public string Action;

        /// <summary>
        /// Stores the ModuleInfo object for the current object
        /// </summary>
        public ModuleInfo ModData { get; set;}

        /// <summary>
        /// Portal ID for this request
        /// </summary>
        public int PortalId { get{ return ModData.PortalID; }}

        /// <summary>
        /// TabId for this request
        /// </summary>
        public int TabId { get{ return ModData.TabID; } }

        /// <summary>
        /// ModuleId for this request
        /// </summary>
        public int ModuleId { get{ return ModData.ModuleID; } }
        
        /// <summary>
        /// Carries the user information for the current request
        /// </summary>
        public UserInfo User { get; set; }

        /// <summary>
        /// Returns the user id of the currently logged in user. Throws an error if the user is not logged in
        /// </summary>
        public int UserId { get
        {
            if (User == null)
                throw new ApplicationException("No user has has been attached to this process");
            return User.UserID;
        } }

        /// <summary>
        /// Creates a properly formatted url with the route parameter built from this RouteInfo object
        /// </summary>
        /// <returns>A url that can be used from the client</returns>
        public string GetMvcUrl()
        {
            var querystring = "";
            foreach (string key in App.Request.QueryString.Keys)
            {
                if (key == Params.QS.Route(ModuleId)) continue;
                querystring += "&" + key + "=" + App.Request.QueryString[key];
            }
            return App.MvcUrl(Action, Controller, PortalId, TabId, ModuleId, querystring, false);
        }

        /// <summary>
        /// Creates a properly formatted url with the route parameter built from this RouteInfo object
        /// </summary>
        /// <returns>A url that can be used from the client</returns>
        public string GetMvcAjaxUrl()
        {
            var querystring = "";
            foreach (string key in App.Request.QueryString.Keys)
            {
                if (key == Params.QS.AjaxRoute(App.ModuleShortName)) continue;
                querystring += "&" + key + "=" + App.Request.QueryString[key];
            }
            return App.MvcAjaxUrl(Controller, Action, PortalId, TabId, ModuleId, querystring);
        }

        /// <summary>
        /// Returns the path to the view based on the controller and action
        /// i.e. controller=home, action=index then the view path returned would be ~/desktopmodules/views/controller/action.ascx
        /// </summary>
        /// <returns>A relative url ~/desktopmodules/views/controller/action.ascx</returns>
        public string ViewPath
        {
            get { return RouteHelper.ControlPath(Controller, Action, App); }
        }
        
        /// <summary>
        /// Creates a new instance of the UrlHelper class to help you create urls
        /// </summary>
        public UrlHelper Url
        {
            get { return _url ?? (_url = new UrlHelper(this)); }
        }
        private UrlHelper _url;

        /// <summary>
        /// This is the heart your DNN module application. It contains your module informaiton, as well as the methods
        /// and helpers you may use throughout your application. It also is the main source for the StaticUrl MvcUrl and ControlUrl
        /// methods that the baseview classes inherit. It also has all the functions inherited from the BigfootWeb.WebApp 
        /// you may use in all ASP.NET applications.
        /// </summary>
        public DnnApp App { get; set; }
    }

}
